package com.jz.util;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * Created by Administrator on 2015/10/26.
 */
public class AttributeSupport {
    public static final Logger LOGGER = LoggerFactory.getLogger(AttributeSupport.class);

    public static String zkConfig = "conf/zkpublisher.properties";
    public static String appConfig = "conf/application.properties";

    public static String syMainConfigFile = System.getProperty("user.dir")
            + "/conf/sy_main_config.xml";
    public static String syAppConfigFile = System.getProperty("user.dir")
            + "/conf/sy_app_config.xml";
    //ZOOKEEPER配置
    public static String HOST_PORT = "192.168.243.77:2181,192.168.243.78:2182,192.168.243.249:2183";
    //主节点名称
    public static String MAIN_NODE = "/main_node";
    //APP节点名称
    public static String APP_NODE = "/app_node";
    //ZOOKEEPER超时时间
    public static int ZK_TIMEOUT = 30000;
    //运行JAR
    public static String exec[];
    public static String MAIN_LEADER = "/MAIN_LEADER";

    public static String TCP_END = "##END##";
    public static String TCP_SERVER_RECEIVED = "##RECEIVED##";

    public static String TCP_HOST = "127.0.0.1";  //要连接的服务端IP地址
    public static int TCP_PORT = 8899;   //要连接的服务端对应的监听端口

    //UDP广播IP或网段
    public static String UDP_HOST = "127.0.0.1";
    //UDP广播端口
    public static int UDP_PORT = 3344;

    public static String HEARTBEAT_IP = "127.0.0.1";
    public static int HEARTBEAT_PORT = 10101;

    public static int SEND_INTERVAL = 5;

    public static int JETTY_PORT = 10010;

    private static CompositeConfiguration config = new CompositeConfiguration();

    static {
        try {
            File f = new File(zkConfig);
            if (f.exists()) {
                config.addConfiguration(new PropertiesConfiguration(zkConfig));
            }
            f = new File(appConfig);
            if (f.exists()) {
                config.addConfiguration(new PropertiesConfiguration(appConfig));
            }
            loadProperties();
        } catch (Exception e) {
            LOGGER.error("配置解析失败" + e.getMessage());
        }
    }

    public static void loadProperties() throws Exception {
        LOGGER.error("加载配置项");
        try {
            HOST_PORT = config.getString("HOST_PORT");
        } catch (Exception e) {
            LOGGER.error("", e);
            throw new Exception(e);
        }
        try {
            ZK_TIMEOUT = config.getInt("ZK_TIMEOUT");
        } catch (Exception e) {
            LOGGER.error("", e);
            throw new Exception(e);
        }
        try {
            MAIN_NODE = config.getString("MAIN_NODE");
        } catch (Exception e) {
            LOGGER.error("", e);
            throw new Exception(e);
        }
        try {
            APP_NODE = config.getString("APP_NODE");
        } catch (Exception e) {
            LOGGER.error("", e);
            throw new Exception(e);
        }
        try {
            String temp = config.getString("EXEC");
            if (temp != null && !temp.trim().equals("")) {
                exec = temp.trim().split("###");
            }
        } catch (Exception e) {
            LOGGER.error("", e);
            exec = null;
            throw new Exception(e);
        }


        try {
            TCP_END = config.getString("TCP_END");
        } catch (Exception e) {
            LOGGER.error("TCP_END加载失败", e);
        }

        try {
            TCP_SERVER_RECEIVED = config.getString("TCP_SERVER_RECEIVED");
        } catch (Exception e) {
            LOGGER.error("TCP_SERVER_RECEIVED加载失败", e);
        }

        try {
            UDP_HOST = config.getString("UDP_HOST");
        } catch (Exception e) {
            LOGGER.error("UDP_HOST加载失败", e);
        }

        try {
            TCP_HOST = config.getString("TCP_HOST");
        } catch (Exception e) {
            LOGGER.error("TCP_HOST加载失败", e);
        }

        try {
            TCP_PORT = config.getInt("TCP_PORT");
        } catch (Exception e) {
            LOGGER.error("TCP_PORT加载失败", e);
        }

        try {
            UDP_PORT = config.getInt("UDP_PORT");
        } catch (Exception e) {
            LOGGER.error("UDP_PORT加载失败", e);
        }

        try {
            HEARTBEAT_IP = config.getString("HEARTBEAT_IP");
        } catch (Exception e) {
            LOGGER.error("HEARTBEAT_IP加载失败", e);
        }

        try {
            HEARTBEAT_PORT = config.getInt("HEARTBEAT_PORT");
        } catch (Exception e) {
            LOGGER.error("HEARTBEAT_PORT加载失败", e);
        }

        try {
            SEND_INTERVAL = config.getInt("SEND_INTERVAL");
        } catch (Exception e) {
            LOGGER.error("SEND_INTERVAL加载失败", e);
            SEND_INTERVAL = 5;
        }

        try {
            JETTY_PORT = config.getInt("JETTY_PORT");
        } catch (Exception e) {
            LOGGER.error("JETTY_PORT加载失败", e);
            JETTY_PORT = 10010;
        }
    }

}
